package org.dant.ant.extension.tasks.commands;

import java.io.*;

import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.utils.FileUtil;
import org.dant.ant.extension.utils.ParameterUtil;

public class CopyCommand extends RemoteCommand {

    private byte[] fileBuffer;
    private String fullSourcePath;
    private String fullDestPath;

    public CopyCommand() {
    }

    public String getName() {
        return CommandsConstants.DANT_CMD_COPY;
    }

    public void init() throws Exception {
        fullSourcePath = FileUtil.resolveFileName((String)parameters.get("srcdir"), (String)parameters.get("filename"));

        int posi = -1;
        int len  = -1;
    
        if (parameters.get("position") != null) {
            posi = ((Integer)parameters.get("position")).intValue();
        }
        if (parameters.get("length") != null) {
            len  = ((Integer)parameters.get("length")).intValue();
        }

        fileBuffer = null;
        fileBuffer = FileUtil.readFile(fullSourcePath,
                                       posi,
                                       len);

        if (parameters.get("rename") != null) {
            fullDestPath = FileUtil.resolveFileName((String)parameters.get("destdir"), (String)parameters.get("rename"));
        } else {
            fullDestPath = FileUtil.resolveFileName((String)parameters.get("destdir"), (String)parameters.get("filename"));
        }
    
        this.parameters.putAll(ParameterUtil.resolve(this, true));
    }

    public byte[] getByteArray() {
        return fileBuffer;
    }
  
  
    public String getFullSourcePath() {
        return fullSourcePath;
    }

    public String getFullDestPath() {
        return fullDestPath;
    }
}
